package conference_service

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"net/http"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/utils/formatTimeUtil"
	"peilian-api/utils/paginate"
	"time"
)

type ConferenceService struct{}

func (co *ConferenceService) ListConferencePage(ctx *gin.Context, cQuery model.ConferenceReq) (int64, interface{}, *errors.ErrRes) {
	allConferenceList := make([]*tables.Conference, 0)
	//var out []interface{}
	var out = make([]model.ConferenceVo, 0)
	aid, exists := ctx.Get("aid")
	if !exists {
		// 服务异常，未获取到当前用户agentId
		return 0, allConferenceList, errors.NewErrInfo(http.StatusInternalServerError, fmt.Errorf("用户id不存在"))
	}
	cQuery.AgentId = aid.(uint)
	allQuery := variable.DB.MySQL.Model(&tables.Conference{})
	var count int64
	tx := allQuery.Count(&count)
	if tx.Error != nil {
		return 0, allConferenceList, errors.NewErrInfo(10002, tx.Error)
	}
	// 查询列表
	if err := variable.DB.MySQL.Scopes(paginate.Paginate(ctx.Request)).Order("created_at desc").Find(&allConferenceList).Error; err != nil {
		return 0, allConferenceList, errors.NewErrInfo(10003, err)
	}
	for _, con := range allConferenceList {
		var vo model.ConferenceVo
		copier.Copy(&vo, con)
		vo.StartTime = formatTimeUtil.TimeToStringTime(con.StartTime)
		vo.EndTime = formatTimeUtil.TimeToStringTime(con.EndTime)
		out = append(out, vo)
	}
	return count, out, nil
}

/**
*@author: yy
*@description: 创建会议
*@date :2024/8/16 下午3:41
**/
func (co *ConferenceService) SaveConference(ctx *gin.Context, cDO model.ConferenceAddDTO) *errors.ErrRes {
	conferenceDO := new(tables.Conference)
	copier.Copy(&conferenceDO, cDO)
	conferenceDO.StartTime = formatTimeUtil.StringTimeToTime(cDO.StartTime)
	conferenceDO.EndTime = formatTimeUtil.StringTimeToTime(cDO.EndTime)
	uid, _ := ctx.Get("aid")
	//  赋值
	cDO.AgentId = uid.(uint)
	// 保存参加的学生信息
	list := cDO.ConferenceApplyList
	if len(list) == 0 {
		return errors.NewErrInfo(http.StatusInternalServerError, fmt.Errorf("请选择参加的学生信息"))
	}
	tx := variable.DB.MySQL.Create(&conferenceDO)

	fmt.Println("获取到创建的新的会议id = {}", conferenceDO.ID)
	// 遍历保存学生信息
	for _, apply := range list {
		fmt.Println("开始循环保存学生报名信息 =======")
		studentApply := new(tables.ConferenceApply)
		studentApply.ConferenceId = conferenceDO.ID
		studentApply.AgentId = uid.(uint)
		studentApply.StudentId = apply.StudentId
		// 根据id查询学生信息
		user, _ := tables.GetUserInfoById(int(apply.StudentId))
		studentApply.Academy = user.Academy
		studentApply.Grade = user.Grade
		studentApply.Sno = user.Sno
		studentApply.Name = user.Name
		studentApply.Profession = user.Profession
		studentApply.Education = user.Education
		err := variable.DB.MySQL.Create(&studentApply).Error
		if err != nil {
			fmt.Println("保存学生报名信息失败，原因 err = {}", err.Error())
		}
	}
	fmt.Println("创建好的 ={}", tx)
	return nil
}

/**
*@author: yy
*@description: 删除会议记录
*@date :2024/8/16 下午4:51
**/
func (co *ConferenceService) DeleteConference(ctx *gin.Context) *errors.ErrRes {
	conferenceId := ctx.Param("conferenceId")
	fmt.Println("获取需要删除的会议信息 ={}", conferenceId)
	// 查询单条记录
	var conference tables.Conference
	d := variable.DB.MySQL.Where("id = ?", conferenceId).Find(&conference)
	if d.RowsAffected == 0 {
		//format.NewResponseJson(ctx).Err(30001, "未查询到会议信息！")
		return errors.NewErrInfo(500, fmt.Errorf("未查询到会议信息"))
	}
	fmt.Println("获取到的会议信息 =={}", conference.ConferenceName)
	variable.DB.MySQL.Delete(&tables.Conference{}, "id = ?", conferenceId)
	return nil
}
func (co *ConferenceService) CancelConference(ctx *gin.Context) *errors.ErrRes {
	conferenceId := ctx.Param("conferenceId")
	fmt.Println("取消会议信息 ={}", conferenceId)
	// 查询单条记录
	var conference tables.Conference
	d := variable.DB.MySQL.Where("id = ?", conferenceId).Find(&conference)
	if d.RowsAffected == 0 {
		return errors.NewErrInfo(500, fmt.Errorf("未查询到会议信息"))
	}
	// 判断会议是否已经开始
	if conference.StartTime.Before(time.Now()) {
		return errors.NewErrInfo(500, fmt.Errorf("会议已经开始，无法取消."))
	}
	conference.ConferenceStatus = "2"
	err := variable.DB.MySQL.Updates(&conference).Error
	if err != nil {
		return errors.NewErrInfo(500, fmt.Errorf("取消失败，请联系管理员"))
	}
	return nil
}
